package se.webbzon.boltzmann.minigame.question;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;

import se.webbzon.boltzmann.debug.Debug;
import se.webbzon.boltzmann.loader.Loader;
import se.webbzon.boltzmann.loader.LoaderException;
import se.webbzon.boltzmann.loader.LoaderUtilityPointer;

public class QuestionLoader implements Loader {
	
	// A collection of resource names related to this loader
	private final static Collection<String> resourceNames = new ArrayList<String>();
			
	static {
		resourceNames.add("front");
		resourceNames.add("question");
		resourceNames.add("back");
	}
	
	// The URL of the question front page
	private URL frontURL, backURL;
	
	// The URLs of the question pages
	private List<URL> questionURLs;
	
	/*============================================================
	Constructors
	============================================================*/
	
	public QuestionLoader() {
		questionURLs = new ArrayList<URL>();
		frontURL = null;
		backURL = null;
	}
	
	/*============================================================
	Public Methods
	============================================================*/
	
	/** Returns URL to the front page. **/
	public URL getFront() {
		return frontURL;
	}
	
	/** Returns URL to the back page. **/
	public URL getBack() {
		return backURL;
	}
	
	/** Returns an unmodifiable list of the URLs to the question pages. **/
	public List<URL> getQuestions() {
		return Collections.unmodifiableList(questionURLs);
	}

	@Override public String getCategory() {
		return "questions";
	}

	@Override public Collection<String> getSupportedResources() {
		return resourceNames;
	}

	@Override public void load(LoaderUtilityPointer pointer, String resourceName,
			Map<String, String> attributeMap) throws LoaderException {
		String page = attributeMap.get("file");
		URL url = null;
		if (page != null) try {
			url = new URL(pointer.getXMLURL(),page);
		} catch (MalformedURLException e) {
			Debug.report(e);
		}
		
		if (resourceName.equals("front"))
			frontURL = url;
		else if (resourceName.equals("back"))
			backURL = url;
		else if (url != null)
			questionURLs.add(url);
	}

}
